const path = require("path");
const { getLoader, loaderByName } = require("@craco/craco");

module.exports = {
  webpack: {
    alias: {
      "@": path.resolve(__dirname, "src"), // 关键：Webpack 别名
    },
    configure: (webpackConfig, { env, paths }) => {
      // 针对 TypeScript 的额外配置（确保别名在 ts-loader 中生效）
      const { isFound, match } = getLoader(
        webpackConfig,
        loaderByName("babel-loader")
      );
      if (isFound) {
        match.loader.include = [
          path.resolve(__dirname, "src"),
          // 如果有其他需要包含的路径，可在此添加
        ];
      }
      return webpackConfig;
    },
  },
  jest: {
    configure: {
      moduleNameMapper: {
        "^@/(.*)$": "<rootDir>/src/$1", // Jest 别名支持
      },
    },
  },
  devServer: {
    // 解决跨域问题，代理多个服务器
    proxy: [
      {
        context: ['/demo', '/mock'],
        target: 'https://www.demo.com', // 服务器mock（纯前端的不需要，它自带拦截请求）
        secure: false, // 忽略证书验证
        changeOrigin: true,
        pathRewrite: {
          '^/demo': '', // /demo/xxx → https://www.demo.com/xxx
          '^/mock': ''
        }
      },
      {
        context: ['/api'],
        target: 'https://snake-mock.smallyogurt.xyz/api',
        secure: false,
        changeOrigin: true,
        pathRewrite: {
          '^/api': '',
        }
      }
    ]
  },
};
